/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package APIs;

import bllobjects.Account;
import dalfactory.SDALFactory;
import fsktms.request.RequestParameters;
import fsktms.request.RequestRespond;
import fsktms.request.api.RequestAPIGroup;
import java.rmi.RemoteException;
import java.util.logging.Level;
import java.util.logging.Logger;
import utils.*;

/**
 *
 * @author salm
 */
public class AccountAPIs extends RequestAPIGroup
{
    public AccountAPIs()
    {
        super("Account");
    }
    
    public RequestRespond getAccountInfo(RequestParameters params)
    {
        RequestRespond resultParams = new RequestRespond();
        String userName = (String) params.get("ID");
        
        try {
            DataTable dt = SDALFactory.getInstance().getAccount().getAccount(userName);
            if (dt.size() > 0)
            {
                DataRow row = dt.get(0);
                resultParams.put(RequestParameters.KEY_RESULT,
                        new Account(row.get("UserID"), row.get("UserName"),
                            Integer.parseInt(row.get("Experience")),
                            Integer.parseInt(row.get("Level")),
                            row.get("TypeID")));
            }
            else
            {
                RequestRespond.setFailure(resultParams, RequestParameters.VALUE_FAILURE_CODE_NOT_FOUND, "Account not found");
            }
        } catch (RemoteException ex) {
            Logger.getLogger(AccountAPIs.class.getName()).log(Level.SEVERE, null, ex);
            RequestRespond.setFailure(resultParams, RequestParameters.VALUE_FAILURE_CODE_SERVER_ERROR, ex.getMessage());
        }
        
        return resultParams;
    }
    
    public RequestRespond getAccount(RequestParameters params)
    {
        RequestRespond resultParams = new RequestRespond();
        String userName = (String) params.get("ID");
        String password = (String) params.get("Password");
        
        try {
            DataTable dt = SDALFactory.getInstance().getAccount().getAccount(userName, password);
            if (dt.size() > 0)
            {
                DataRow row = dt.get(0);
                resultParams.put(RequestParameters.KEY_RESULT,
                        new Account(row.get("UserID"), row.get("UserName"),
                            Integer.parseInt(row.get("Experience")),
                            Integer.parseInt(row.get("Level")),
                            row.get("TypeID")));
            }
            else
            {
                RequestRespond.setFailure(resultParams, RequestParameters.VALUE_FAILURE_CODE_NOT_FOUND, "Account not found");
            }
        } catch (RemoteException ex) {
            Logger.getLogger(AccountAPIs.class.getName()).log(Level.SEVERE, null, ex);
            RequestRespond.setFailure(resultParams, RequestParameters.VALUE_FAILURE_CODE_SERVER_ERROR, ex.getMessage());
        }
        
        return resultParams;
    }
    
    public RequestRespond changeAccountPassword(RequestParameters params)
    {
        RequestRespond resultParams = new RequestRespond();
        String userName = (String) params.get("ID");
        String oldPassword = (String) params.get("OldPassword");
        String newPasword = (String) params.get("NewPassword");
        
        try {
            boolean result = SDALFactory.getInstance().getAccount().changePassword(userName, oldPassword, newPasword);
            if (result)
            {
                resultParams.setResult(true);
            }
            else
            {
                RequestRespond.setFailure(resultParams, RequestParameters.VALUE_FAILURE_CODE_NOT_FOUND, "Account not found or password is incorrect");
            }
        } catch (RemoteException ex) {
            Logger.getLogger(AccountAPIs.class.getName()).log(Level.SEVERE, null, ex);
            RequestRespond.setFailure(resultParams, RequestParameters.VALUE_FAILURE_CODE_SERVER_ERROR, ex.getMessage());
        }
        
        return resultParams;
    }

    public RequestRespond isAccountExist(String userName)
    {
        RequestRespond resultParams = new RequestRespond();
        
        try {
            resultParams.put(RequestParameters.KEY_RESULT, SDALFactory.getInstance().getAccount().isAccountExist(userName));
        } catch (RemoteException ex) {
            Logger.getLogger(AccountAPIs.class.getName()).log(Level.SEVERE, null, ex);
            RequestParameters.setFailure(resultParams, RequestParameters.VALUE_FAILURE_CODE_SERVER_ERROR, ex.getMessage());
        }
        
        return resultParams;
    }

    public RequestRespond register(RequestParameters params)
    {
        RequestRespond resultParams = new RequestRespond();
        String userName = (String) params.get("ID");
        String password = (String) params.get("Password");
        String name = (String) params.get("Name");
        try {
            
            if (SDALFactory.getInstance().getAccount().insertAccount(userName, password, name))
            {
                return getAccount(new RequestParameters()
                        .set("ID", userName)
                        .set("Password", password));
            }
            
            RequestParameters.setFailure(resultParams, RequestParameters.VALUE_FAILURE_CODE_SERVER_ERROR, "Can't insert to server");
        } catch (RemoteException ex) {
            Logger.getLogger(AccountAPIs.class.getName()).log(Level.SEVERE, null, ex);
            RequestParameters.setFailure(resultParams, RequestParameters.VALUE_FAILURE_CODE_SERVER_ERROR, ex.getMessage());
        }
        
        return resultParams;
    }
}
